<?php

namespace App\Http\Controllers\admin;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Validator;

/**
* 商品销售类型
*/
class SaleController extends Controller
{
    /**
     * 展示销售策略列表
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex()
    {
        //查询所有销售模块
        $data = DB::table('goods_sale') -> get();

        //显示销售模块列表
        return view('admin/sale/index',['data' => $data]);

    }


    /**
     * 显示当前模块下商品列表
     *
     * @return \Illuminate\Http\Response
     */
    public function getGoodsindex(Request $request ,$id)
    {
         //接收分页 搜索数据
        $title = $request -> input('title','');
        $page = $request -> input('page',10);
       
        //接收参数数组
        $var = [];
        $var['title'] = $title;
        $var['page'] = $page;
        
        //查询数据库
        $data = DB::table('goods')
            ->join('goods_brand', 'goods.br_id', '=', 'goods_brand.id')
            ->join('goods_class', 'goods.cl_id', '=', 'goods_class.id')
            ->join('goods_sale', 'goods.sa_id', '=', 'goods_sale.id')
            ->join('goods_version', 'goods.id', '=', 'goods_version.gid')
            ->select('goods_version.id as ve_id','goods_version.num as num','goods_version.ctime as ctime','goods_version.title as version','goods.*', 'goods_brand.title as brand', 'goods_class.title as class','goods_sale.title as sale')
            -> where('goods.title','like','%'.$title.'%') 
            -> where('goods.sa_id' ,'=' ,$id)
            -> paginate($page);
       
       //解析模板
       return view('admin/sale/goodsindex',['data' => $data,'var' => $var,'sa_id' => $id]);
    }

    /**
     * 添加页面.
     *
     * @return \Illuminate\Http\Response
     */
    public function getAdd()
    {
        //显示添加销售模块视图
        return view('admin/sale/add');

    }


    /**
     * 编辑当前销售模块.
     *
     * @return \Illuminate\Http\Response
     */
    public function postUpdate(Request $request,$id)
    {
        

        //显示添加销售模块视图
        return view('admin/sale/add');

    }

    /**
     * 添加页面.
     *
     * @return \Illuminate\Http\Response
     */
    public function postInsert(Request $request)
    {
        //接收数据
        $input = $request -> all();

        //设置过滤规则
        $rules = [
            'title' => 'required',
        ];
        
        //设置错误消息
        $messages = [
                 'title.required' => '销售标题必填',
        ];

        //验证数据
        $validator = Validator::make($input,$rules,$messages);
       
        if ($validator->fails()) {
            return back()
                        ->withErrors($validator)
                        ->withInput();
        }

        //去除无用字段
        $res = $request -> only('title','discount','position','content');

        //存入该条数据
        $res = DB::table('goods_sale') -> insert($res);
        
        //返回结果
        return self::res($res,'添加','/admin/sale/index','/admin/sale/index');
    }


    /**
    * 添加商品到销售模块
    */
    public function getGoodsadd($sa_id)
    {
        //查询所有类型
        $data = DB::table('goods_class')
                     ->select(DB::raw("concat(path,',',id) as paths,title,id,status"))
                     ->orderBy('paths')
                     ->get();
        
        //格式化字端title
        foreach ($data as $k => $v) {

            $c = substr_count($data[$k]['paths'], ',');
            
            $str = str_repeat('|---', $c);
            $data[$k]['title'] = $str.$data[$k]['title'];
        }

        //显示添加列表
        return view('admin/sale/goodsadd',['data' => $data, 'sa_id' => $sa_id]);
    }


    /**
    * 修改销售模块所属商品
    */
    public function postGoodsupdate(Request $request, $sa_id)
    {
        //过滤数据
        if (!$request -> has( 'id')) {
            return back();
        }
        
        //接收数据
        $id = $request -> input( 'id');

        // 返回模板
       DB::table('goods_sale') -> where('id',$id) -> update(['sa_id',$sa_id]);
    }


    /**
    * 修改销售模块所属商品
    */
    public function getDelete($id)
    {
        //删除该条数据
       $res = DB::table('goods_sale') -> where('id',$id) -> delete();
    
        //返回结果
        return self::res($res,'删除','/admin/sale/index','/admin/sale/index');
    }


     /**
    * 停止促销该商品
    */
    public function getGoodsupdate(Request $request ,$id)
    {
        $sa_id = $request -> input('sa_id');

        //更新该条商品
       $res = DB::table('goods') -> where('id',$id) -> update(['sa_id' => 0]);
    
        //返回结果
        return self::res($res,'移出商品','/admin/sale/goodsindex/'.$sa_id,'/admin/sale/goodsindex/'.$sa_id);
    }

    /**
    * 响应检索商品ajax
    */
    public function getAjax($cl_id){
      
        $res = DB::table('goods') -> where('cl_id', $cl_id) -> get();
        
        return response()->json($res);
    } 

     /**
    * 处理结果函数
    */
     static public function res($res,$message,$url_su,$url_er){
        if ($res) {
         
           //跳转到员工列表并显示成功信息
           return redirect($url_su)
                        ->with(['message'=>$message.'成功','type'=>1]);
        }else{

           //跳转到添加页面
           return redirect($url_er)
                        ->with(['message'=>$message.'失败','type'=>2]);
        }
    }
}
